package com.tdw.eurekaclient.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheKey;
import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheRemove;
import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import com.tdw.eurekaclient.dto.UserInfo;

@RestController
public class HystrixCacheRequestController {

	@Autowired
	private RestTemplate restTemplate;
	//
	//
	// @CacheResult(cacheKeyMethod = "getCacheKey")
	// @HystrixCommand(commandKey = "findUserById", groupKey = "UserService",
	// threadPoolKey = "userServiceThreadPool")
	// @RequestMapping("/hystrix/findById")
	// public UserInfo findById(Long id) {
	// ResponseEntity<UserInfo> user =
	// restTemplate.getForEntity("http://hello-service/hystrix/getUserById?id={id}",
	// UserInfo.class, id);
	// return user.getBody();
	// }
	//
	//
	//
	// public String getCacheKey(Long id) {
	// return String.valueOf(id);
	// }
	//
	// @CacheResult
	// @HystrixCommand(commandKey = "findUserById", groupKey = "UserService",
	// threadPoolKey = "userServiceThreadPool")
	// @RequestMapping("/hystrix/findById2")
	// public UserInfo findById2(@CacheKey("id") Long id) {
	// ResponseEntity<UserInfo> user =
	// restTemplate.getForEntity("http://hello-service/hystrix/getUserById?id={id}",
	// UserInfo.class, id);
	// return user.getBody();
	// }
	//
	//
	// @CacheRemove(commandKey = "findUserById")
	// @HystrixCommand(commandKey = "updateUser",groupKey =
	// "UserService",threadPoolKey = "userServiceThreadPool")
	// public void updateUser(@CacheKey("id")UserInfo user){
	// restTemplate.postForObject("http://hello-service/hystrix/getUserById",user,UserInfo.class);
	// }

	@CacheResult
	@HystrixCommand
	@RequestMapping("/hystrix/findById")
	public UserInfo getUserByid(Long id) {
		return restTemplate.getForObject("http://hello-service/hystrix/getUserById?id={id}", UserInfo.class, id);
	}
}
